<?php
/**
 * Created by PhpStorm.
 * 云联惠回调处理
 * User: fengbob
 * Date: 2017/11/9
 * Time: 15:16
 */
namespace app\controllers;

use app\librarys\BaseApiController;
use app\librarys\getui\GetuiClientMerchant;
use app\models\Merchant;
use YunLianHui\OAuth2;


class OauthController extends BaseApiController
{
//    const SessionKey = 'session_key_merchant_code';
    /**
     * 云联惠用户建泉回调地址
     */
    public function actionCallback()
    {
        $code = \Yii::$app->request->get('code');
        $push_id = \Yii::$app->request->get('state');
        echo 'code:'.$code.' state:'.$push_id;
        //获取云联惠token
        $ylh_token = Merchant::getAccessToken($code);
//         echo '<>1<>';
        if($ylh_token == null)
        {
            //push app login error.
            GetuiClientMerchant::pushMessageToSingle($push_id,'login error!');
            return;
        }
//        echo '<>2<>token:'.$ylh_token;

        //获取云联惠用户信息
        $user = Merchant::getYLHUserInfo($ylh_token);
        if($user == null)
        {
            return;
        }
        $phone = $user['mobile'];
        $item = Merchant::getByPhone($phone);
//        echo '3';

        if($item == null)//新用户，注册
        {
            Merchant::register($phone);
        }
//        echo '<>4<>';

//        $item = Merchant::login($phone);
        $code = Merchant::getYlhCode($phone);
//        $token = $item['token'];
        $result = '{"code":"'.$code.'" ,"phone":"'.$phone.'"}';
//        $result = [
//            'merchant_token' => $token,
//            'phone' => $phone
//        ];
        echo 'token:'.$code;
        echo '<>5<>';

        Merchant::updateYlhUserInfo($phone,$ylh_token,$user['member_id']);
        GetuiClientMerchant::pushMessageToSingle($push_id,$result);
    }

    /**
     * 直接返回token
     */
    public function actionCallback_bak()
    {
        $code = \Yii::$app->request->get('code');
        $push_id = \Yii::$app->request->get('state');
        echo 'code:'.$code.' state:'.$push_id;
        //获取云联惠token
        $ylh_token = Merchant::getAccessToken($code);
//         echo '<>1<>';
        if($ylh_token == null)
        {
            //push app login error.
            GetuiClientMerchant::pushMessageToSingle($push_id,'login error!');
            return;
        }
//        echo '<>2<>token:'.$ylh_token;

        //获取云联惠用户信息
        $user = Merchant::getYLHUserInfo($ylh_token);
        if($user == null)
        {
            return;
        }
        $phone = $user['mobile'];
        $item = Merchant::getByPhone($phone);
//        echo '3';

        if($item == null)//新用户，注册
        {
            Merchant::register($phone);
        }
//        echo '<>4<>';

        $item = Merchant::login($phone);
        $token = $item['token'];
        $result = '{"merchant_token":"'.$token.'" ,"phone":"'.$phone.'"}';
//        $result = [
//            'merchant_token' => $token,
//            'phone' => $phone
//        ];
        echo 'token:'.$token;
        echo '<>5<>';

        Merchant::updateYlhUserInfo($phone,$ylh_token,$user['member_id']);
        GetuiClientMerchant::pushMessageToSingle($push_id,$result);
    }


    public function actionTestGetAccessToken()
    {
        $request = \Yii::$app->request;
        $code = $request->get('code');
        Merchant::getAccessToken($code);
    }


}